package com.github.binarywang.demo.spring.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.binarywang.demo.spring.builder.TextBuilder;
import com.github.binarywang.demo.spring.service.WeixinService;

import io.renren.entity.Addovertime;

import io.renren.entity.SearchcommandfindPersonandstation;
import io.renren.entity.WeixinUser;
import io.renren.service.AddovertimeService;
import io.renren.service.SearchcommandfindPersonandstationService;
import io.renren.service.WeixinUserService;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 
 * @author Binary Wang
 *
 */
@Component
public class MsgHandler extends AbstractHandler {
	@Autowired
	private SearchcommandfindPersonandstationService searchcommandfindPersonandstationService;
	@Autowired
	private AddovertimeService addovertimeService;
	@Autowired
	WeixinUserService weixinUserService;
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
			WxSessionManager sessionManager) {

		WeixinService weixinService = (WeixinService) wxMpService;

		String wxContent = wxMessage.getContent();
		// 查找当前人员
		String patternr = "^[r|R]\\s+(\\d{0,9}|[\u4e00-\u9fa5]{0,4})$";
		boolean isMatchr = Pattern.matches(patternr, wxContent);
		// 查找当前基站人员
		String patternw = "^[w|W]\\s+(\\d{0,9}|[\u4e00-\u9fa5]{0,4})$";
		boolean isMatchw = Pattern.matches(patternw, wxContent);
		// 加班
		String patternj = "^[j|J]\\s+[1|2|3|4]\\s+[0-9]{0,3}$";
		boolean isMatchj = Pattern.matches(patternj, wxContent);
		// 上班
		String patterns = "^[s|S]\\s*$";
		boolean isMatchs = Pattern.matches(patterns, wxContent);
		// 下班
		String patternx = "^[x|X]\\s*$";
		boolean isMatchx = Pattern.matches(patternx, wxContent);
		if (isMatchr) {
			String[] strs = wxContent.split("\\s+");

			for (String str : strs) {
				logger.info("str={}", str);
			}
			Map<String, Object> map = new HashMap<>();
			map.put("val", strs[1]);
			List<SearchcommandfindPersonandstation> list = searchcommandfindPersonandstationService.queryPerson(map);

			String content = "****当前人员列表****\n";
			for (SearchcommandfindPersonandstation p : list) {

				content += "" + p.getNamePersonnel() + "-" + p.getCodeTag() + "在" + p.getNameStation() + "\n";

			}

			return new TextBuilder().build(content, wxMessage, weixinService);
		} else if (isMatchw) {
			String[] strs = wxContent.split("\\s+");

			for (String str : strs) {
				logger.info("str={}", str);
			}
			Map<String, Object> map = new HashMap<>();
			map.put("val", strs[1]);
			List<SearchcommandfindPersonandstation> list = searchcommandfindPersonandstationService.queryStation(map);

			String content = "****当前基站人员列表****\n";
			for (SearchcommandfindPersonandstation s : list) {

				content += "" + s.getNamePersonnel() + "-" + s.getCodeTag() + "在" + s.getNameStation() + "\n";

			}

			return new TextBuilder().build(content, wxMessage, weixinService);
		} else if (isMatchj) {
			String[] strs = wxContent.split("\\s+");

			for (String str : strs) {
				logger.info("str={}", str);
			}
			Map<String, Object> usermap = new HashMap<>();
			usermap.put("openid", wxMessage.getFromUser());
			List<WeixinUser> userlist = weixinUserService.openidlist(usermap);
			if (userlist != null && !userlist.isEmpty()) {
			if(userlist.get(0).getRoleid().intValue()==2){
			Map<String, Object> map = new HashMap<>();
			map.put("id_input", strs[1]);
			map.put("val", strs[2]);
			List<Addovertime> list = addovertimeService.addtime(map);

			return new TextBuilder().build(list.get(0).getMsg(), wxMessage, weixinService);
				}
			return new TextBuilder().build("您无此权限", wxMessage, weixinService);
			}
		} else if (isMatchs) {
		
			Map<String, Object> map = new HashMap<>();
			map.put("openid", wxMessage.getFromUser());
			List<WeixinUser> userlist = weixinUserService.openidlist(map);
			if (userlist != null && !userlist.isEmpty()) {
				if(userlist.get(0).getRoleid().intValue()==2){
				WeixinUser user = weixinUserService.queryObject(userlist.get(0).getId());
				user.setGowork(1);
				weixinUserService.update(user);
				return new TextBuilder().build("您已修改成上班状态", wxMessage, weixinService);
				}
			} else {
				return new TextBuilder().build("请注册用户", wxMessage, weixinService);
			}
		} else if (isMatchx) {
	
			Map<String, Object> map = new HashMap<>();
			map.put("openid", wxMessage.getFromUser());
			List<WeixinUser> userlist = weixinUserService.openidlist(map);
			if (userlist != null && !userlist.isEmpty()) {
				if(userlist.get(0).getRoleid().intValue()==2){
				WeixinUser user = weixinUserService.queryObject(userlist.get(0).getId());
				user.setGowork(2);
				weixinUserService.update(user);
				return new TextBuilder().build("您已修改成下班状态", wxMessage, weixinService);
				}
			} else {
				return new TextBuilder().build("请注册用户", wxMessage, weixinService);
			}
		}

		// TODO 组装回复消息
		String content = "指令错误,请查看用户公告！";
		return new TextBuilder().build(content, wxMessage, weixinService);

	}

}
